﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using MixScript.Appl.Session;
using MixScript.Appl.Security.Crypto;
using MixScript.BLL.ApplAdmin;





namespace MixScript.Appl.Security
{
    public class cSecurity
    {
        private cMenu oMenu;
        
        private static bool _bAuth;
        private  cAES_Cryptography oCrypt;

        public  bool BAuth
        {
            get { return cSecurity._bAuth; }
            set { cSecurity._bAuth = value; }
        }
     


        public cSecurity()
        {
            oMenu = new cMenu();
            oCrypt = new cAES_Cryptography();

        }


        private bool isCorrectPassword(string spass, string storedpass)
        {
            try
            {   
                if (spass == oCrypt.decryptString(storedpass, cAES_Cryptography.encKeys))
                {
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool changePAss(string suid, string newPass)
        {
            try
            {
               cUserManagement oUMG = new cUserManagement();
               if (oUMG.ChangeUserPAss(suid, newPass))
               {
                   return true;
               }
               else
               {
                   return false;
               }
                return true;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool DoLogin(string sUID, string sPass, out bool mustChangePassword)
        {

            try
            {

                mustChangePassword = false;
              cUserManagement oUMG = new cUserManagement();
                DataTable dt = new DataTable();

                if (oUMG.getUser(ref dt, sUID))
                {
                    if (isCorrectPassword(sPass, dt.Rows[0]["userpassword"].ToString()))
                    {
                        new cApplSession().userID = sUID;
                        if (dt.Rows[0]["passexpired"].ToString() == "1")
                        {
                            mustChangePassword = true;
                        }
                        return true;
                    }
                    else
                    {
                        throw new Exception("Login Gagal - Password Anda Salah");
                    }
                }
                else
                {
                    throw new Exception("User Tidak Terdaftar");
                }

                if (sUID.ToLower() == "admin")
                {
                    return true;
                }
                else
                {
                    return false;
                }
                return true;

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }



        }
        public bool getAuthMenu(ref DataTable dt)
        {
            try
            {
                return oMenu.getAuthMenu(ref dt);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }



    }
}
